"""
主窗口模块
"""
from PyQt6.QtWidgets import QMainWindow, QWidget, QVBoxLayout, QHBoxLayout, QMessageBox
from .widgets.left_menu_widget import LeftMenuWidget
from .widgets.content_widget import ContentWidget
from .widgets.top_info_widget import TopInfoWidget


class MainWindow(QMainWindow):
    """主窗口类"""
    
    def __init__(self):
        super().__init__()
        self.setWindowTitle("悠悠捡漏助手")
        self.setGeometry(100, 100, 1200, 800)
        
        # 创建中央部件
        central_widget = QWidget()
        self.setCentralWidget(central_widget)
        
        # 创建主布局
        main_layout = QVBoxLayout(central_widget)
        main_layout.setContentsMargins(0, 0, 0, 0)
        main_layout.setSpacing(0)
        
        # 创建顶部信息区域
        self.top_info = TopInfoWidget()
        main_layout.addWidget(self.top_info)
        
        # 创建下方的水平布局（左侧菜单和右侧内容区域）
        bottom_layout = QHBoxLayout()
        bottom_layout.setContentsMargins(0, 0, 0, 0)
        bottom_layout.setSpacing(0)
        
        # 创建左侧菜单
        self.left_menu = LeftMenuWidget()
        bottom_layout.addWidget(self.left_menu)
        
        # 创建右侧内容区域
        self.content = ContentWidget()
        bottom_layout.addWidget(self.content)
        
        # 设置左侧菜单和右侧内容的宽度比例
        bottom_layout.setStretch(0, 1)  # 菜单占1
        bottom_layout.setStretch(1, 4)  # 内容区域占4
        
        # 将下方布局添加到主布局
        main_layout.addLayout(bottom_layout)
        
        # 设置顶部和下方区域的高度比例
        main_layout.setStretch(0, 1)  # 顶部信息区域占1
        main_layout.setStretch(1, 4)  # 下方区域占4
        
        # 连接信号
        self.left_menu.menu_item_clicked.connect(self.content.switch_to_page)
    
    def closeEvent(self, event):
        """
        重写关闭事件，添加确认对话框防止误操作
        """
        reply = QMessageBox.question(
            self,
            '确认退出',
            '确定要退出应用程序吗？\n退出后将停止所有后台任务。',
            QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No,
            QMessageBox.StandardButton.No
        )
        
        if reply == QMessageBox.StandardButton.Yes:
            event.accept()
        else:
            event.ignore()
